package demo.android.com.mvpdemo.RxJava;


import android.util.Log;

/**
 * Created by Vya on 2018/1/24 0024.
 */

public class ObservableJust<T> extends Observable<T> {
    private T item;
    public ObservableJust(T item) {
        this.item = item;
    }

    @Override
    public void subscribeActual(Observer<T> observer) {

        Log.e("wangyan","ObservableJust--->>subscribeActual");
        //通过代理对象调用

 /*       observer.onSubscribe();
        try {
            observer.onNext(item);
        }catch (Exception e){
            observer.onError(e);
        }
        observer.onComplete();*/
        ScalarDisposable sd = new ScalarDisposable(observer,item);
        observer.onSubscribe();
        sd.run();
    }


    private class ScalarDisposable<T>{
        private Observer observer;
        private T item;


        public ScalarDisposable(Observer<T> observer, T item) {
            this.observer = observer;
            this.item = item;
        }

        public void run() {

            try {
                observer.onNext(item);
                observer.onComplete();
            }catch (Exception e){
                observer.onError(e);
            }
        }
    }


}
